package com.xueblog.pub;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.http.ResponseEntity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 封装
 * 处理HTTP响应
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BlogResponseEntity implements Serializable {

    public static ResponseEntity<Map<String, Object>> ok() {
        HashMap<String, Object> body = new HashMap<>();
        body.put(Constants.STATUS, Constants.HTTP_200);
        body.put(Constants.RESPONSE_TEXT, Constants.OK);
        return ResponseEntity.ok(body);
    }

    public static ResponseEntity<Map<String, Object>> ok(Map<String, Object> body) {
        if (Objects.isNull(body)) {
            body = new HashMap<>(4);
        }
        if (!body.containsKey(Constants.STATUS)) {
            body.put(Constants.STATUS, Constants.HTTP_200);
            body.put(Constants.RESPONSE_TEXT, Constants.OK);
        }
        return ResponseEntity.ok(body);
    }

    public static ResponseEntity<Map<String, Object>> error() {
        HashMap<String, Object> body = new HashMap<>(4);
        if (!body.containsKey(Constants.STATUS)) {
            body.put(Constants.STATUS, Constants.HTTP_500);
            body.put(Constants.RESPONSE_TEXT, Constants.FAIL);
        }
        return ResponseEntity.ok(body);
    }

    public static ResponseEntity<Map<String, Object>> success(Object retData) {
        HashMap<String, Object> responseBody = new HashMap<>(4);
        responseBody.put(Constants.STATUS, Constants.HTTP_200);
        responseBody.put("data", retData);
        return ResponseEntity.ok(responseBody);
    }
}
